package com.example.extensions.listener;

import com.example.extensions.constant.Constants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class QosListener {
   @RabbitListener(queues = Constants.QOS_QUEUE)
   public void handMessage(Message message, Channel channel) throws Exception {
       long deliveryTag = message.getMessageProperties().getDeliveryTag();
       try {
            //消费者逻辑
           System.out.printf("[normal.queue] 1接收到消息: %s, deliveryTag: %d \n", new String(message.getBody(), "UTF-8"), deliveryTag);

//           进行业务逻辑处理
//           System.out.println(("业务逻辑处理"));
           Thread.sleep(2000);
//           肯定确认
           channel.basicAck(deliveryTag, false);
       } catch (Exception e) {
           //否定确认
           channel.basicNack(deliveryTag, false, false); //requeue 为false, 消息变为死信
       }
   }
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void handMessage2(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //消费者逻辑
            System.out.printf("[normal.queue] 2接收到消息: %s, deliveryTag: %d \n", new String(message.getBody(), "UTF-8"), deliveryTag);

            //进行业务逻辑处理
//            System.out.println(("业务逻辑处理"));
            //肯定确认
            Thread.sleep(1000);
           channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            //否定确认
            channel.basicNack(deliveryTag, false, false); //requeue 为false, 消息变为死信
        }
    }
}
